<?php

/**
 * page actions.
 *
 * @package    DUYTAN
 * @subpackage page
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class rateListAction extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executerateList(sfWebRequest $request)
  {
	$this->rate_form = new AdminRateForm();
	$qv = Doctrine_Query::create()
			->from('VTNSVariable v')
			->where('variable_type = 0');
	$variables = $qv->fetchArray();
	$this->variable_names = array();
	foreach ($variables as $variable)
	{
		$this->variable_names[$variable['variable_id']] = $variable['variable_name'];
	}
	
	if ($request->hasParameter('create') && $request->getParameter('create') == 'Create') {
		$res = $request->getParameter('rate'); // get form rate
		$this->is_create_action = true;
		if ($res['variable_id']) {
			$rate = new VTNSRate();
			$rate->variable_id = $res['variable_id'];
			$rate->min_pax = $res['min_pax'];
			$rate->max_pax = $res['max_pax'];
			$rate->rate_value = $res['rate_value'];
			if ($res['is_active'] && $res['is_active'] == 'on')
				$rate->is_active = 1;
			else
				$rate->is_active = 0;
			
			try {
				$rate->save();
				$this->create_success = true;
			} catch (sfException $e) {
				echo $e->getMessage();
			}
		}
		
	} else {
		
		$q = Doctrine_Query::create()
			    ->from('VTNSRate c');
		$res = $request->getParameter('rate');
		$q = $this->execFilers($q, $res);
		$this->sort_by = $request->getParameter('sort_by', 'created_at');
    	if ($request->getParameter('sort_order', 1))
        	$this->sort_order = 'desc';
    	else
        	$this->sort_order = 'asc';
			
    	if (Doctrine_Core::getTable('VTNSRate')->hasField($this->sort_by))
        	$q->orderBy('c.'.$this->sort_by.' '.strtoupper($this->sort_order));
			
		$this->pager = new sfDoctrinePager('VTNSRate', 10000);
		$this->pager->setQuery($q);
    	$this->pager->setPage($request->getParameter('page', 1));
    	$this->pager->init();
		if ($request->isXmlHttpRequest()) {
        	return 'Ajax';
    	} else return SfView::SUCCESS;
	}
  }


	private function execFilers($q, $res)
	{
		return $q;
	}
}
